﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RigDraw : MonoBehaviour
{
    public Mesh meshBone;
    public Mesh meshJoint;
    public Material material;
    public List<Transform> ts;
    public void Init(Transform rig)
    {
        ts = rig.GetTransforms();
    }
    void Update()
    {
        Vector3 s = Vector3.one;
        for (int i = 0; i < ts.Count; i++)
        {
            if (meshBone != null)
            {
                var pos = ts[i].position;
                var q = ts[i].rotation;
                if (i < ts.Count - 1)
                {
                    var len = ts[i + 1].localPosition.magnitude;
                    s = len * Vector3.one;
                    var m = Matrix4x4.TRS(pos, q, s);
                    Graphics.DrawMesh(meshBone, m, material, 0);
                    Graphics.DrawMesh(meshJoint, m, material, 0);
                }
                else // tail
                {
                    var m = Matrix4x4.TRS(pos, q, s);
                    Graphics.DrawMesh(meshJoint, m, material, 0);
                }
            }
            if (ts[i] == null) { Destroy(gameObject); return; }
        }
    }
}
